package org.surthi.app.bank.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.surthi.app.bank.service.AccountService;
import org.surthi.app.bank.web.bean.FixDepositView;

public class FixDepositController extends MultiActionController {

	private AccountService accountService;
	
	@Override
	public void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		// TODO Auto-generated method stub		
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat,true));
		binder.registerCustomEditor(Double.class,new CustomNumberEditor(Double.class, true));
		binder.registerCustomEditor(List.class, new CustomCollectionEditor(List.class));
		super.initBinder(request, binder);
	}
	
	public ModelAndView viewFixDeposit(HttpServletRequest request,HttpServletResponse response	)throws Exception {	
		ModelAndView mav = new ModelAndView();
		FixDepositView fixDepositView = new FixDepositView();
		if (accountService != null ){
			fixDepositView = accountService.displayAccountHolderInformation("SBH208");
			System.out.println("Account Number : "+ fixDepositView.getAccountId());
			System.out.println("Ac Holder Name :"+ fixDepositView.getAccountHolderName());
			System.out.println("Bank Name : "+ fixDepositView.getBankName());
			System.out.println("Total accounts : "+fixDepositView.getAccountIds().size());
		}
		mav.setViewName("fixdeposit");
		mav.addObject("fixdeposit",fixDepositView);
		return mav;
	}
	public ModelAndView saveFixDeposit(HttpServletRequest request,HttpServletResponse response,FixDepositView fixDepositView )throws Exception {
		ModelAndView mav = new ModelAndView();
		accountService.saveAccountHolderInformation(fixDepositView);
		
		fixDepositView = accountService.displayAccountHolderInformation("SBH208");
		mav.setViewName("fixdeposit");	
		mav.addObject("fixdeposit",fixDepositView);
		
		return mav;
	}

	public AccountService getAccountService() {
		return accountService;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}
}
